﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrossesGrav.GameLogic;
using System.Drawing;

namespace CrossesGrav
{
    class Cell
    {
        private int a;
        private int x;
        private int y;

        public struct Status
        {
            bool cross;
            bool circle;
            bool acessibility;
            bool free;
            bool canTouch;


            public bool CanTouch
            {
                get
                {
                    return canTouch;
                }
            }
            public bool Cross
            {
                get
                {
                    return cross;
                }
                set
                {
                    if (value == true)
                    {
                        Circle = false;
                        Free = false;
                    }
                    cross = value;
                }
            }
            public bool Circle
            {
                get
                {
                    return circle;
                }
                set
                {
                    if (value == true)
                    {
                        Cross = false;
                        Free = false;
                    }
                    circle = value;
                }
            }
            public bool Acessibility
            {
                get
                {
                    return acessibility;
                }
                set
                {
                    acessibility = value;
                    if (free == value) canTouch = value;
                }
            }
            public bool Free
            {
                get
                {
                    return free;
                }
                set
                {
                    if (value)
                    {
                        Circle = false;
                        Cross = false;
                    }
                    free = value;
                    if (acessibility == value) canTouch = value;
                }
            }

        }
        public struct Watch
        {
            public bool nord;
            public bool west;
            public bool east;
            public bool n_east;
            public bool n_west;
        }
        private Sides n = new Sides();
        private Sides e = new Sides();
        private Sides w = new Sides();
        private Sides s = new Sides();
        public Status stat = new Status();
        public Watch watchTo = new Watch();
        public PictureBox Box = new PictureBox();
        List<Player> plrs = new List<Player>();
        public List<Cell> SideList = new List<Cell>();

        Player p1;
        Player p2;

        public Cell()
        {

        }

        public Cell(int h, int w, List<Player> pls)
        {
            plrs = pls;
            p1 = pls[0];
            p2 = pls[1];
            Box.Width = w;
            Box.Height = h;
            n.Parent = this;
            e.Parent = this;
            s.Parent = this;
            this.w.Parent = this;
            Box.Image = Game.images["Free"];
            Box.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            Box.MouseClick += new MouseEventHandler(Box_Click);
            Box.Cursor = System.Windows.Forms.Cursors.Hand;

            ////////////////
           // Box.BorderStyle = BorderStyle.FixedSingle;
            ////////////////

            this.watchTo.east = true;
            this.watchTo.n_east = true;
            this.watchTo.n_west = true;
            this.watchTo.nord = true;
            this.watchTo.west = true;
            Box.Show();


        }

        public void Box_Click(object sender, MouseEventArgs e)
        {
            /////////////////////////////
          /*  if ((e.Button == MouseButtons.Right) && (Game.test))
            {
                Form TestForm = new Form();
                TestForm.Height = 600;
                TestForm.Width = 400;gf
                TestForm.Show();
                TextBox t = new TextBox();
                t.Multiline = true;
                t.Parent = TestForm;
                t.Dock = DockStyle.Fill;
                t.ScrollBars = ScrollBars.Both;
                if (this.stat.Cross)
                    TestForm.Controls[0].Text = Game.logs_O[this.Number];
                else
                    TestForm.Controls[0].Text = Game.logs_X[this.Number];

                TestForm.Text = "Лог по клетке #" + this.Number;

            }
            else*/
                ////////////////////////////
                if (e.Button == MouseButtons.Left)
                {
                    Player Now_play = null;
                    bool check;

                    if ((p1.moving == true) && (p1.can_click == true))
                        Now_play = p1;
                    else
                        if ((p2.moving == true) && (p2.can_click == true))
                            Now_play = p2;

                    try
                    {

                        check = Now_play.moving;
                        Marker(this, Now_play);
                        if (check == Now_play.moving)
                            Now_play.Move(Now_play.u);
                    }
                    catch
                    {
                        //ignore click;
                    }
                }
        }


        public void Marker(Cell a, Player p)
        {
            if (a.stat.Acessibility == true)
            {
                if (p.side == true)
                {
                    //a.Box.Image = global::CrossesGrav.Properties.Resources.Cross;
                    a.Box.Image = Game.images["Cross"];
                    a.stat.Cross = true;
                }
                else
                {
                   // a.Box.Image = global::CrossesGrav.Properties.Resources.Circle;
                    a.Box.Image = Game.images["Circle"];
                    a.stat.Circle = true;
                }
                a.stat.Acessibility = false;
                if (a.Nord.obj != null)
                {
                    a.Nord.obj.stat.Acessibility = true;
                    Game.AvalibleCells[Game.AvalibleCells.IndexOf(a)] = a.Nord.obj;
                }
                else
                    Game.AvalibleCells.Remove(a);
                p1.u--;
                p2.u--;
                p1.moving = !p1.moving;
                p2.moving = !p2.moving;
                p.LastCell = a;

                Game.movements--;

                if (p.CheckForWin(a) == true)
                {
                    MessageBox.Show(p.name + " побеждает!");

                   // if (p == Program.f.Player1)
                    GC.Collect();
                    Game.GameOver = true;
                    /////////////////////////
                   //Program.f.pictureBox6_Click(new object(), new EventArgs());
                    ///////////////////////////
                }
                if (p.GetType().Name == "Human")
                Game.lets_game(plrs);
            }
            else
            {
                if (a.South.obj != null)
                    if (a.South.obj.stat.Free == true)
                    {
                        while (a.stat.Acessibility != true)
                        {
                            a = a.South.obj;
                        }
                        Marker(a, p);
                    }
            }
        }

        public int Number
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }

        }
        public int X_Coord
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
                Box.Left = x;
            }

        }
        public int Y_Coord
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
                Box.Top = y;
            }

        }



        public Sides Nord
        {
            get
            {
                return n;
            }
            set
            {
                n.obj = value.obj;
                n.Connected = value.Connected;
                value.obj.South.obj = n.Parent;
                value.obj.South.Connected = true;
            }
        }
        public Sides West
        {
            get
            {
                return w;
            }
            set
            {
                w.obj = value.obj;
                w.Connected = value.Connected;
            }
        }
        public Sides East
        {
            get
            {
                return e;
            }
            set
            {
                e.obj = value.obj;
                e.Connected = value.Connected;
                value.obj.West.obj = e.Parent;
                value.obj.West.Connected = true;

            }
        }
        public Sides South
        {
            get
            {
                return s;
            }
            set
            {
                s.obj = value.obj;
                s.Connected = value.Connected;
                value.obj.Nord.obj = s.Parent;
                value.obj.Nord.Connected = true;
            }
        }
        public Cell N_East
        {
            get
            {
                if (this.e.obj != null)
                {
                    if (this.e.obj.n.obj != null)
                        return this.e.obj.n.obj;
                    else return null;
                }
                if (this.n.obj != null)
                {
                    if (this.n.obj.e.obj != null)
                        return this.n.obj.e.obj;
                    else return null;
                }
                else return null;
            }
        }
        public Cell N_West
        {
            get
            {
                if (this.w.obj != null)
                {
                    if (this.w.obj.n.obj != null)
                        return this.w.obj.n.obj;
                    else return null;
                }
                if (this.n.obj != null)
                {
                    if (this.n.obj.w.obj != null)
                        return this.n.obj.w.obj;
                    else return null;
                }
                else return null;
            }
        }
        public Cell S_East
        {
            get
            {
                if (this.e.obj != null)
                {
                    if (this.e.obj.s.obj != null)
                        return this.e.obj.s.obj;
                    else return null;
                }
                if (this.s.obj != null)
                {
                    if (this.s.obj.e.obj != null)
                        return this.s.obj.e.obj;
                    else return null;
                }
                else return null;
            }
        }
        public Cell S_West
        {
            get
            {
                if (this.w.obj != null)
                {
                    if (this.w.obj.s.obj != null)
                        return this.w.obj.s.obj;
                    else return null;
                }
                if (this.s.obj != null)
                {
                    if (this.s.obj.w.obj != null)
                        return this.s.obj.w.obj;
                    else return null;
                }
                else return null;
            }
        }
    }

}